package edu.upc.clase.demo.dao.impl;
import edu.upc.clase.demo.dao.ArmadoSalaDao;
import edu.upc.clase.demo.entity.ArmadoSala;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Lourdes
 */


@Repository
public class ArmadoSalaDaoImpl extends SimpleJdbcDaoSupport implements ArmadoSalaDao {
     
    private static Logger log = LoggerFactory.getLogger(ArmadoSalaDao.class);

    @Autowired
    public ArmadoSalaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    @Override
    public Integer registrar(ArmadoSala armadosala) {

        getJdbcTemplate().update(
               "insert into Armado(idarmado,sala_idsala,instrumento_idinstrumento,reserva_idreserva) values (?,?,?,?)",
              armadosala.getidArmado(),armadosala.getidSala() ,armadosala.getidInstrumento(), armadosala.getidReserva());
       return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
   
           }
    @Override
    public void modificararmado(ArmadoSala armadosala){
        
        getJdbcTemplate().update(
                "update Armado set sala_idsala = ?, instrumento_idinstrumento = ?, reserva_idreserva=?  where idarmado = ?",
                armadosala.getidSala(),armadosala.getidInstrumento(),armadosala.getidReserva(),armadosala.getidArmado());
                
        }
    
    @Override
    public void eliminarArmado(ArmadoSala armadosala){
      
        getJdbcTemplate().update(
               "delete from Armado where idArmado = ?", armadosala.getidArmado()  );
        
    }

   
        @Override
    public List<ArmadoSala> buscarTodos(){
        return getSimpleJdbcTemplate().query(
                "SELECT idarmado , sala_idsala, instrumento_idinstrumento, reserva_idreserva FROM Armado",
                new BeanPropertyRowMapper<ArmadoSala>(ArmadoSala.class));
    }
    
        @Override
        public ArmadoSala buscarArmado(Integer idarmado){
                  try {
            return getSimpleJdbcTemplate().queryForObject(
                    "select sala_idsala, instrumento_idinstrumento, reserva_idreserva from Armado where idArmado=?",
                    new BeanPropertyRowMapper<ArmadoSala>(ArmadoSala.class), idarmado);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
        }
}



    
